package com.hqd527.qdshop.serviceproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqd527.qdshop.product.*;
import com.hqd527.qdshop.serviceproduct.mapper.*;
import com.hqd527.qdshop.serviceproduct.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 太优秀
 * @date 2022/2/4 22:14
 * @description
 */
@Service
public class ManageServiceImpl implements ManageService {

    // autowired是spring的；resource是java自己的
    @Resource
    private BaseCategory1Mapper baseCategory1Mapper;

    @Resource
    private BaseCategory2Mapper baseCategory2Mapper;

    @Resource
    private BaseCategory3Mapper baseCategory3Mapper;

    @Resource
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Resource
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private SpuInfoMapper spuInfoMapper;


    /**
     * 查询 一级分类列表
     * @return
     */
    @Override
    public List<BaseCategory1> getBaseCategory1List() {
        List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(null);
        return baseCategory1List;
    }

    /**
     * 根据 一级分类ID 查询 二级分类列表
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseCategory2> getBaseCategory2List(Long category1Id) {
        // select * from gmall_product where category1_id = category1Id
        // MybatisPlus构建查询条件
        QueryWrapper<BaseCategory2> category2QueryWrapper = new QueryWrapper<BaseCategory2>();
        category2QueryWrapper.eq("category1_id", category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(category2QueryWrapper);
        return baseCategory2List;
    }

    /**
     * 根据 二级分类ID 查询 三级分类列表
     * @param category2Id
     * @return
     */
    @Override
    public List<BaseCategory3> getBaseCategory3List(Long category2Id) {
        // select * from gmall_product where category2_id = category2Id
        QueryWrapper<BaseCategory3> category3QueryWrapper = new QueryWrapper<BaseCategory3>();
        category3QueryWrapper.eq("category2_id", category2Id);
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(category3QueryWrapper);
        return baseCategory3List;
    }

    /**
     * 根据 分类ID 查询 属性名列表 （可以根据一级分类ID / 二级分类ID / 三级分类ID 查询）
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getBaseAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        // 多表关联查询，需要自定义查询方法，然后到xml编写SQL实现
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.selectBaseAttrInfoList(category1Id, category2Id, category3Id);
        return baseAttrInfoList;

    }


    /**
     * 【重难点】修改/增加 属性对象
     * @param baseAttrInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class) // 如果有异常会回滚
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {

        // 【1】先修改/新增平台属性名
        if(baseAttrInfo.getId() != null){
            baseAttrInfoMapper.updateById(baseAttrInfo);
        }else{
            baseAttrInfoMapper.insert(baseAttrInfo);
        }

        // 【2】再保存平台属性值
        // 由于不知道当前执行的保存操作是新增还是修改，所以干脆就先把当前属性id所对应的属性值信息从属性值表中删除，然后再新增进去
        QueryWrapper<BaseAttrValue> attrValueQueryWrapper = new QueryWrapper<BaseAttrValue>();
        attrValueQueryWrapper.eq("attr_id", baseAttrInfo.getId()); // 属性名id就是属性值中的attr_id
        // 根据属性id去属性值表中删除对应的属性值信息
        baseAttrValueMapper.delete(attrValueQueryWrapper);

        // 【3】保存属性值列表数据
        if(baseAttrInfo != null){
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            for (BaseAttrValue attrValue : attrValueList) {
                // 【设置属性名id】 属性名表的id 就是 属性值表的attr_id
                attrValue.setAttrId(baseAttrInfo.getId());

                baseAttrValueMapper.insert(attrValue);
            }
        }
    }

    /**
     * 根据属性id获取属性值列表
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getBaseAttrValueList(Long attrId) {
        QueryWrapper<BaseAttrValue> attrValueQueryWrapper = new QueryWrapper<>();
        attrValueQueryWrapper.eq("attr_id", attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(attrValueQueryWrapper);
        return baseAttrValueList;
    }


    /**
     * 根据属性id获取属性对象
     * @param attrId
     * @return
     */
    @Override
    public BaseAttrInfo getAttrInfo(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        if(baseAttrInfo != null){
            List<BaseAttrValue> attrValueList = getBaseAttrValueList(attrId);
            if(!CollectionUtils.isEmpty(attrValueList)){
                baseAttrInfo.setAttrValueList(attrValueList);
            }
        }
        return baseAttrInfo;
    }

    /**
     * 根据输入的SPU信息分页查询与之匹配的SPU列表
     * @param current
     * @param limit
     * @param spuInfo
     * @return
     */
    @Override
    public IPage<SpuInfo> getSpuInfoByPage(Long current, Long limit, SpuInfo spuInfo) {

        QueryWrapper<SpuInfo> spuInfoWrapper = new QueryWrapper<>();

        // 【封装分页参数pageParams】
        Page<SpuInfo> pageParams = new Page<>(current,limit);

        // 根据商品的三级ID分页查询
        spuInfoWrapper.eq("category3_id",spuInfo.getCategory3Id());
        spuInfoWrapper.orderByDesc("id");

        return spuInfoMapper.selectPage(pageParams,spuInfoWrapper);

    }


}
